#include <main.h>
#include <ds1307.c>
#include <2416.C>
#int_TIMER1

int16 gValtimer1=1200;
int16 order[3]={0},cmd[4]={0xaa};
BYTE gCount,gYear=0,gMth=0,gDay=0,gDow=0,gHou=0,gMin=0,gSec=0;
void  TIMER1_isr(void) 
{

}

#int_TIMER2
void  TIMER2_isr(void) 
{

}

#int_CCP1
void  CCP1_isr(void) 
{
   
    
    gValtimer1=(get_timer1()/75);//(get_timer1()*0.8)/60
    set_timer1(0);
    rtc_set_time(9,59,00);
    output_high(PIN_A0);
    put_byte(0xaa);
    delay_ms(2000);
    output_low(PIN_A0);
 
    
    

   /*gValtimer1=(get_timer1()/75);//(get_timer1()*0.8)/60
  set_timer1(0);
  if(gCount++>30){gCount=0;
   rtc_get_time(gHou,gMin,gSec);
   rtc_get_date(gDay,gMth,gYear,gDow);
  }*/
}

void main()
{
   //int16 secDelay,minDelay,houDelay;
   
  // unsigned int valTimer2;
  int8 i=0;
  
 

   //setup_adc_ports(NO_ANALOGS|VSS_VDD);
   //setup_adc(ADC_CLOCK_DIV_2);
   //setup_spi(SPI_SS_DISABLED);
   //setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  
   //setup_timer_2(T2_DIV_BY_1,0,1);
   
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   rtc_init(0,1,0);
   init_ext_eeprom();
   
   //enable_interrupts(INT_TIMER2);
   setup_ccp1(CCP_CAPTURE_RE);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
   
   
   //enable_interrupts(INT_TIMER1);
   enable_interrupts(INT_CCP1);
   enable_interrupts(GLOBAL);
 

   //TODO: User Code
 
   cmd[0]=0xff;
   cmd[1]=0xaa;
   cmd[2]=0x55;
   while(1){
   gMin=0;
   rtc_get_time(&gHou,&gMin,&gSec);
   //rtc_get_time(gHou,gMin,gSec);
    //output_high(PIN_A0);
    output_high(PIN_A0);
     put_byte(gMin);
   delay_ms(1000);
   output_low(PIN_A0);
   delay_ms(1000);    

   output_high(PIN_E2);
    put_byte(gHou);
   delay_ms(1000);
   output_low(PIN_E2);
   delay_ms(1000);    
    
  // output_high(PIN_A0);
  // cmd[0]=1>>5;
  // put_byte(cmd[0]); 
  // display_frame(cmd);
   /*computeDelayTime(& secDelay,& minDelay,& houDelay, gHou, gMin,gSec );
     sort_3(order,secDelay,minDelay,houDelay);
     delay_us(order[0]);
     if(order[0]== secDelay)
     //display second
     //display();
     if(order[0]== minDelay)
     //display minute
     //display();
     if(order[0]== houDelay)
     //display hour
     //display();
     
     
     delay_us(order[1]- order[0]);
     if(order[1]== secDelay)
     //display second
     //display();
     if(order[1]== minDelay)
     //display minute
     //display();
     if(order[1]== houDelay)
     //display hour
     //display();
     
     delay_us(order[2]- order[1]);
     if(order[2]== secDelay)
     //display second
     //display();
     if(order[2]== minDelay)
     //display minute
     //display();
     if(order[2]== houDelay)
     //display hour
     //display();
     valTimer2=get_timer2();
     delay_us(60*gValtimer1 - valTimer2);*/
   }

}
void boundary(){
int8 i=0;
    for(i=0;i<12;i++){
     output_high(PIN_A0);
     put_byte(0x01);
     delay_us(gValtimer1*5);
     put_byte(0xD);
     put_byte(0x01);
     }
     i=get_timer1()*0.8;
     
     delay_us(gValtimer1*60 - i );
     
     output_low(PIN_A0);
}
void computeDelayTime(int16 *secDelay,int16 *minDelay,int16 *houDelay,BYTE gHou,BYTE gMin,BYTE gSec ){
      *secDelay=(int16) gSec*gValtimer1; 
      *minDelay=(int16) gMin*gValtimer1; 
      *houDelay=(int16) (5*gHou*gValtimer1 + (gMin*gValtimer1)/10);   
}

void swapint(int16 *a, int16 *b)
{
   int16 temp;
   
   temp = *a;
   *a = *b;
   *b = temp;
   }

void sort_3( int16 order[], int16 hour, int16 min, int16 sec)
{
   order[0]=hour;
   order[1]=min;
   order[2]=sec;
   
   if(order[0] > order[1])
   {
      swapint(&order[0],&order[1]);
   }
   if(order[1] > order[2])
   {
      swapint(&order[1],&order[2]);
   }
   if(order[0] > order[1])
   {
      swapint(&order[0],&order[1]);
   }
}
void create_second_hand(BYTE cmd[]){
   int8 i=0;
   for(i=0;i<13;i++)
      generate_data(i,RED,cmd);
      
   
}
void create_minute_hand(BYTE cmd[]){
   int8 i=0;
   for(i=0;i<7;i++)
      generate_data(i,REDGREEN,cmd);
      
   
}
void create_hour_hand(BYTE cmd[]){
   int8 i=0;
   for(i=0;i<10;i++)
      generate_data(i,GREEN,cmd);
      
   
}
void generate_data(int8 leds_name,int8 color_value,BYTE cmd[]){
      int8 i=0;
      if(leds_name <= LED_D4)
         i=0;
      else if(leds_name > LED_D4 && leds_name <= LED_D8)
         i=1;
      else if(leds_name > LED_D8 && leds_name <= LED_D12)
         i=2;
      else 
         i=3;
      if(color_value == BLACK)
      {
         //cmd[i]:0=0;
         //cmd[i]:1=0;
      } 
      //????????????????????????????????
         
      
      

}
void display_frame(BYTE cmd[]){
 
     output_high(PIN_A0);
     put_byte(cmd[0]); 
     
     //output_low(PIN_A0);
     
     output_high(PIN_E0);
     put_byte(cmd[1]);    
     
     //output_low(PIN_E0);
     
     output_high(PIN_E1);
     put_byte(cmd[2]);    
     
    // output_low(PIN_E1);
     
     output_high(PIN_E2);
     put_byte(cmd[3]);    
    
     //output_low(PIN_E2);
     
         
}
void put_byte(BYTE cmd){
   //int i=0;
   //for(i=0;i<8;i++)
   //   output_bit(PIN_D0+i,shift_right(&cmd,1,0));
   output_d(cmd);
   delay_ms(1000);
   //output_d (0x00);
   
}
